//
//  adDetailViewController.m
//  试验
//
//  Created by Tiancheng on 16/10/31.
//  Copyright © 2016年 Tiancheng. All rights reserved.
//

#import "adDetailViewController.h"
#import "adDetailView.h"
#import "adDetailModel.h"
#import "UILabel+LabelHeightAndWidth.h"
#import "Macros.h"
#import "adModel.h"
#import "HttpRequest.h"
#import "allActivity.h"
#import "xianXiaDetailViewController.h"
#import "SVProgressHUD.h"

@interface adDetailViewController ()
//header
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIView *headerView01;
//标题
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
//背景scrollView
@property (weak, nonatomic) IBOutlet UIScrollView *backScrollView;
//backGroundView
@property (weak, nonatomic) IBOutlet UIView *backGroundView;
//关于爱贝斯的图片的数组
@property(nonatomic,copy)NSMutableArray *describlePicArray;
//我们的环境的图片的数组
@property(nonatomic,copy)NSMutableArray *environmentalPicArray;
//我们的优点的图片的数组
@property(nonatomic,copy)NSMutableArray *meritsPicArray;
@property(nonatomic,copy)NSDictionary *dataArray;

@property(nonatomic,assign)float adv_height;
@property(nonatomic,assign)float adv_height01;
@property(nonatomic,assign)float adv_height02;
//type和product_name
@property(nonatomic,copy)NSString *merchant_typeString;
@property(nonatomic,copy)NSString *product_nameString;

@property(nonatomic,copy)NSDictionary *diction;
@property(nonatomic,retain)UIView *backView;
@end

@implementation adDetailViewController
@synthesize describlePicArray,environmentalPicArray,meritsPicArray,dataArray,adv_height,adv_height01,adv_height02,diction;
-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden=YES;
    self.tabBarController.tabBar.hidden=YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.headerView.backgroundColor=[UIColor colorWithRed:244/255.0 green:142/255.0 blue:177/255.0 alpha:1];
    self.headerView01.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
    describlePicArray=[NSMutableArray array];
    environmentalPicArray=[NSMutableArray array];
    meritsPicArray=[NSMutableArray array];
    dataArray=[NSDictionary dictionary];
    [self getDataFromNet];
    [SVProgressHUD showWithStatus:@"加载中请稍后..."];
    self.backView=[[UIView alloc]initWithFrame:CGRectMake(0, 20, screenWidth, screenHeight-20)];
    self.backView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:self.backView];
}
//从服务器获取数据
-(void)getDataFromNet
{
    adModel *adm=[adModel sharedManager];
    NSString*urlString=[NSString stringWithFormat:@"http://112.74.114.1/Parenting/index.php/api/Carousel/detail"];
    NSMutableDictionary *parameters=[NSMutableDictionary dictionary];
    parameters[@"cid"]=adm.cidString;
    HttpRequest *httpRequest=[HttpRequest sharedInstance];
    [httpRequest postWithURLString:urlString parameters:parameters success:^(id responseObject)
     {
         id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
         dataArray=[json objectForKey:@"data"];
         self.merchant_typeString=[dataArray objectForKey:@"merchant_type"];
         self.product_nameString=[dataArray objectForKey:@"product_name"];
         if (dataArray.count>0)
         {
//             [self setAdAboutUI];
             [self dismiss];
         }
         //获取数组
//         NSLog(@"json=%@",json);
     } failure:^(NSError *error) {
         NSLog(@"error=%@",error);
     }];
}
- (void)dismiss
{
    [self.backView removeFromSuperview];
    [self setAdAboutUI];
    [SVProgressHUD dismiss];
}
//关于
-(void)setAdAboutUI
{
    NSString *titleString=[dataArray objectForKey:@"merchant_name"];
    self.titleLabel.text=titleString;
    NSString *goodsNameString=[NSString stringWithFormat:@"关于%@",titleString];
    NSString *contentString=[dataArray objectForKey:@"describle"];
    describlePicArray=[dataArray objectForKey:@"describle_pic"];
    adDetailModel *adM=[adDetailModel modelWithString:goodsNameString content:contentString pic:describlePicArray];
    adDetailView *adv=[[adDetailView alloc]init];
    adv.adModel=adM;
    adv.frame=CGRectMake(0, 0, screenWidth, 400);
    [self.backGroundView addSubview:adv];
    [adv addpic:describlePicArray];
    adv_height=adv.frame.origin.y+adv.frame.size.height;
    [self setAdEnvironmentUI];
}
//我们的环境
-(void)setAdEnvironmentUI
{
    NSString *goodsNameString=[NSString stringWithFormat:@"我们的环境"];
    NSString *contentString=[dataArray objectForKey:@"environmental"];
    environmentalPicArray=[dataArray objectForKey:@"describle_pic"];
    adDetailModel *adM=[adDetailModel modelWithString:goodsNameString content:contentString pic:environmentalPicArray];
    adDetailView *adv=[[adDetailView alloc]init];
    adv.adModel=adM;
    adv.frame=CGRectMake(0, adv_height, screenWidth, 400);
    [self.backGroundView addSubview:adv];
    [adv addpic:environmentalPicArray];
    adv_height01=adv.frame.origin.y+adv.frame.size.height;
    [self setAdMeritsUI];
}
//我们的优点
-(void)setAdMeritsUI
{
    NSString *goodsNameString=[NSString stringWithFormat:@"我们的优点"];
    NSString *contentString=[dataArray objectForKey:@"merits"];
    meritsPicArray=[dataArray objectForKey:@"merits_pic"];
    adDetailModel *adM=[adDetailModel modelWithString:goodsNameString content:contentString pic:meritsPicArray];
    adDetailView *adv=[[adDetailView alloc]init];
    adv.adModel=adM;
    adv.frame=CGRectMake(0, adv_height01, screenWidth, 400);
    [self.backGroundView addSubview:adv];
    [adv addpic:meritsPicArray];
    adv_height02=adv.frame.origin.y+adv.frame.size.height+40;
    
    UIButton *btn=[[UIButton alloc]initWithFrame:CGRectMake(screenWidth/2-0.28*screenWidth, adv_height02, 200, 40)];
    [btn setTitle:@"进入商品" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1]];
    int num=[self.merchant_typeString intValue];
    btn.tag=num;
    diction=[NSDictionary dictionaryWithObjectsAndKeys:self.product_nameString,self.merchant_typeString, nil];
    [btn addTarget:self action:@selector(onAdClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.backGroundView addSubview:btn];
    
//    UIView *btn_View=[[UIView alloc]initWithFrame:CGRectMake(screenWidth/2-0.28*screenWidth, adv_height02, 200, 40)];
//    btn_View.backgroundColor=[UIColor colorWithRed:240/255.0 green:98/255.0 blue:146/255.0 alpha:1];
//    int num=[self.merchant_typeString intValue];
//    btn_View.tag=num;
//    [self.backGroundView addSubview:btn_View];
//    //添加手势
//    UITapGestureRecognizer *tap=[[UITapGestureRecognizer  alloc ] initWithTarget:self action : @selector (GoodsDetailInformation:)];
//    btn_View.userInteractionEnabled=YES;
//    [btn_View addGestureRecognizer:tap];
    
    float adv_height03=btn.frame.origin.y+btn.frame.size.height+20;
    //定出他的最终高度是多少
    self.backScrollView.contentSize=CGSizeMake(0, adv_height03);

}
//-(void)GoodsDetailInformation:(UITapGestureRecognizer *)sender
//{
//    int num=(int)sender.view.tag;
//    NSString *str=[NSString stringWithFormat:@"%d",num];
//    NSString *str1=[diction objectForKey:str];
//    allActivity *al=[allActivity shardeManager];
//    al.num=100;
//    al.product_nameString=str1;
//    NSLog(@"al.product_nameString=%@",al.product_nameString);
//
//}
-(void)onAdClick:(UIButton*)sender
{
    NSLog(@"1111111");
    int num=(int)sender.tag;
    NSString *str=[NSString stringWithFormat:@"%d",num];
    NSString *str1=[diction objectForKey:str];
    allActivity *al=[allActivity shardeManager];
    al.num=100;
    al.product_nameString=str1;
    NSLog(@"al.product_nameString=%@",al.product_nameString);
    
    NSUserDefaults *userDefaults=[NSUserDefaults standardUserDefaults];
    NSString *str2=[NSString stringWithFormat:@"%d",al.num];
    [userDefaults setObject:str2 forKey:@"aa.num"];
    xianXiaDetailViewController *bsvc=[xianXiaDetailViewController new];
    [self.navigationController pushViewController:bsvc animated:YES];
    NSLog(@"进入商品");
    
    
}
- (IBAction)goBack:(id)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
